/**
 * Created by necis.zhang on 2016/07/01.
 */
'use strict';

var gulp = require('gulp'),
    browserSync = require('browser-sync').create(),
    uglify = require('gulp-uglify'),
    pump = require('pump'),
    autoprefixer = require('gulp-autoprefixer'),
    cleanCSS = require('gulp-clean-css'),
    imagemin = require('gulp-imagemin'),
    del = require('del'),
    htmlmin = require('gulp-htmlmin'),
    reload = browserSync.reload;

gulp.task('serve:js',function () {
    return gulp.src('src/scripts/**/*.js')
        .pipe(gulp.dest('.tmp/scripts'))
        .pipe(reload({stream: true}));
});

//启动服务
gulp.task('serve', function () {
    // Serve files from the root of this project
    browserSync.init({
        server: {
            baseDir: "src"
        },
        browser:"Chrome"
    });
    gulp.watch("gulpfile.js").on("change", reload);
    gulp.watch("src/*.html").on("change", reload);
    gulp.watch("src/css/*.css").on("change", reload);
    gulp.watch("src/js/*.js").on("change", reload);
});

//压缩CSS
gulp.task('minify-css', function () {
    return gulp.src('src/css/*.css') //压缩的文件
        .pipe(autoprefixer({
            browsers: ['last 35 versions'],
            cascade: false
        }))
        .pipe(cleanCSS({compatibility: 'ie8'}))//执行压缩
        .pipe(gulp.dest('dist/css')); //输出文件夹
});

//压缩js
gulp.task('minify-js', function (cb) {
    pump([
            gulp.src('src/js/**/**'),
            uglify(),
            gulp.dest('dist/js')
        ],
        cb
    );
});

//压缩图片
gulp.task('images', function () {
    return gulp.src('src/images/**/**')
        .pipe(imagemin({optimizationLevel: 3, progressive: true, interlaced: true}))
        .pipe(gulp.dest('dist/images'));
});


//压缩html
gulp.task('minify', function () {
    return gulp.src('src/*.html')
        .pipe(htmlmin({collapseWhitespace: true}))
        .pipe(gulp.dest('dist'));
});


//删除压缩文件
gulp.task('clean', function (cb) {
    return del(['dist'], cb);
});


gulp.task('jquery',function () {
    return gulp.src('src/bower_components/jquery/dist/jquery.min.js')
        .pipe(gulp.dest('dist/bower_components/jquery/dist'));
});

gulp.task('bootstrap',function () {
    return gulp.src('src/bower_components/bootstrap/dist/css/bootstrap.min.css')
        .pipe(gulp.dest('dist/bower_components/bootstrap/dist/css'));
});
gulp.task('font-awesome',function () {
    return gulp.src('src/bower_components/font-awesome/css/font-awesome.min.css')
        .pipe(gulp.dest('dist/bower_components/font-awesome/css/'));
});
gulp.task('fonts',function () {
    return gulp.src('src/bower_components/font-awesome/fonts/**')
        .pipe(gulp.dest('dist/bower_components/font-awesome/fonts/'));
});


//默认任务
gulp.task('default', ['clean'], function () {
    gulp.start('minify-css', 'minify-js', 'images', 'minify','jquery','bootstrap','font-awesome','fonts');
});